//
//  STGoodsTopView.m
//  CuiBoStore
//
//  Created by 崔博 on 16/12/12.
//  Copyright © 2016年 崔博. All rights reserved.
//

#import "STGoodsTopView.h"

@interface STGoodsTopView()

@property (nonatomic, strong) UIView *linView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIButton *seletedBtn;
@end

@implementation STGoodsTopView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleArray = @[@"热门",@"价格",@"好评",@"新品"];
        [self addSubviews];
    }
    return self;
}
#pragma mark - method
// 添加子控件
- (void)addSubviews
{
    CGFloat w = SCREEN_WIDTH / self.titleArray.count;
    for (int i = 0; i < self.titleArray.count; i++) {
        UIButton *btn = [STUIKitTools buttonTitle:self.titleArray[i] font:14 action:@selector(btnClick:) titleColor:[UIColor blackColor] target:self];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        CGFloat x = w * i;
        btn.tag = i + 1;
        btn.frame = CGRectMake(x, 0, w, 28);
        [self addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            self.seletedBtn = btn;
        }
    };
    [self addSubview:self.linView];
}
#pragma mark - event
- (void)btnClick:(UIButton *)sender
{
    // 改变按钮状态
    self.seletedBtn.selected = NO;
    sender.selected = YES;
    self.seletedBtn = sender;
    [UIView animateWithDuration:0.5 animations:^{
        self.linView.centerX = sender.centerX;
    }];
    
    if (self.block) {
        self.block(sender.tag - 1);
    }
}
#pragma mark - lazy
- (UIView *)linView
{
    if (!_linView) {
        _linView = [[UIView alloc] init];
        _linView.frame = CGRectMake(0, 28, SCREEN_WIDTH / 4, 2);
        _linView.backgroundColor = [UIColor orangeColor];
    }
    return _linView;
}
@end
